#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 7

char instr[N+1] = {'\0'};
char dt[N];
int basesrc = 0, basedest = 0;

int M = 0;

int ch2d(char ch)
{
    if(ch >= '0' && ch <= '9')
    {
        return ch - '0';
    }
    else
    {
        return ch - 'A' + 10;
    }
}

char d2ch(int n)
{
    if(n < 10)
    {
        return n + '0';
    }
    else
    {
        return n - 10 +'A';
    }
}

void getM()
{
    int i = 0;
    M = 0;
    for(i = 0; i < strlen(instr); i++)
    {
        M = M * basesrc + ch2d(instr[i]);
    }
}

void decimal2N(int base)
{
    int k = 0;
    do
    {
        dt[k++] = d2ch(M % base);
        M /= base;
    }while(k < N && M != 0);
    if(k >= N && M != 0)
    {
        printf("  ERROR\n");
    }
    else
    {
        for(k = N-1; k >= 0; k--)
        {
            printf("%c", dt[k]);
        }
        printf("\n");
    }
}

void process()
{
    memset(dt, ' ', sizeof(dt[0])*N);
    getM();
    decimal2N(basedest);
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);

    while(1)
    {
        if(scanf("%s%d%d", instr, &basesrc, &basedest) == EOF)
        {
            break;
        }
        process();
    }

    return 0;
}
